"""
top250中同一导演的作品数量top10
"""

import pymongo
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType


db = pymongo.MongoClient('localhost', 27017)
douban = db['doubantop250']
item_info = douban['movietop250']

director_list = []
for i in item_info.find():
    # 有的电影有多个导演，中间使用/隔开，先进行分割
    if '/' in i['director']:
        a = i['director'].split(' / ')
        director_list.append(a[0])
        director_list.append(a[1])
    else:
        director_list.append(i['director'])

# 有的导演执导了多部电影，先进行去重
director_list_new = list(set(director_list))

# 已经去重的director_list_new当作字典的key，在未去重的director_list统计数量用作value
director_dict = {}
for i in director_list_new:
    # 统计导演拍电影的数量
    director_dict[i.strip()] = director_list.count(i)


# director_dict本质上是一个列表
director_dict = sorted(director_dict.items(), key=lambda item: item[1], reverse=True)

director_name = []
movie_num = []

for i in director_dict:
    director_name.append(i[0])
    movie_num.append(i[1])


bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK, width='1200px', height='600px'))
bar.add_xaxis([x for x in director_name[:10]])
bar.add_yaxis("导演作品数量top10", [x for x in movie_num[:10]], category_gap='60%')
bar.set_series_opts(label_opts=opts.LabelOpts(font_size=12))
bar.set_global_opts(
    xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15, font_size=12,)),
    title_opts=opts.TitleOpts(title="top250电影中导演作品数量top10",
    subtitle="数据来源：https://movie.douban.com/top250")
)

bar.render('mydirector.html')